unit IndexTreeMdl;

interface
uses Classes, forms, comctrls, SysUtils, patterns, indexDef;

type
  TModelIndexTree = class(TObservable)
  private
    fRoot: TCommand;
  public
    procedure MakeIndexTree;
    procedure refresh;
    constructor Create;
    destructor Destroy; override;
    function getNodeByParam(AParam: PIndexNode): TCommand;
    procedure nodeSelected(ANodeName: string);
  end;

implementation
uses commandlist;

procedure TModelIndexTree.nodeSelected(ANodeName: string);
//var
//  node: TCommand;
//  function findTheNode(ANode: TCommand; AName: string): TCommand;
//  var
//    i: integer;
//  begin
//    result := nil;
//    if Anode = nil then
//      exit
//    else if ANode.getCommandTxt = AName then
//      result := ANode
//    else
//    begin
//      for i := 0 to Anode.getChildCount - 1 do
//      begin
//        result := findTheNode(Anode.getChildAt(i), AName);
//        if assigned(result) then
//          break;
//      end;
//    end;
//  end;
begin
//  node := findTheNode(self.FRoot, ANodeName);
//  if assigned(node) then
  self.Notify(TCommand.create(CMD_NODE_SELECT, nil, nil, ANodeName));
end;

function TModelIndexTree.getNodeByParam(AParam: PIndexNode): TCommand;
  function equalNode(node1, node2: PIndexNode): boolean;
  begin
    result := assigned(node1) and assigned(node2) and (node1.ID = node2.ID) and
      (node1.name = node2.name) and (node1.ImageIndex = node2.ImageIndex);
  end;

  function findTheNode(ANode: TCommand; AParam: PIndexNode): TCommand;
  var
    i: integer;
  begin
    result := nil;
    if Anode = nil then
      exit
    else if equalNode(ANode.getParamRecord, AParam) then
      result := ANode
    else
    begin
      for i := 0 to Anode.getChildCount - 1 do
      begin
        result := findTheNode(Anode.getChildAt(i), AParam);
        if assigned(result) then
          break;
      end;
    end;
  end;
begin
  result := findTheNode(self.fRoot, aparam);
end;

procedure TModelIndexTree.refresh;
begin
end;

procedure TModelIndexTree.MakeIndexTree;
var
  p: PIndexNode;
  node: TCommand;
begin
  new(p);
  p.ID := '1';
  p.Name := 'My Home';
  p.ImageIndex := 0;
  fRoot := TCommand.Create('My Home', p, nil, '', nil, true);
  fRoot.AutoRelease := false; //manual release

  new(p);
  p.ID := '2';
  p.Name := 'Father';
  p.ImageIndex := 1;
  node := TCommand.Create('Father', p, nil, '', nil, true);
  node.AutoRelease := false; //manual release
  fRoot.Add(Node);

  new(p);
  p.ID := '3';
  p.Name := 'Mother';
  p.ImageIndex := 2;
  node := TCommand.Create('Mother', p, nil, '', nil, true);
  node.AutoRelease := false;
  fRoot.Add(Node);

  new(p);
  p.ID := '4';
  p.Name := 'Children';
  p.ImageIndex := 3;
  node := TCommand.Create('Children', p, nil, '', nil, true);
  node.AutoRelease := false;
  fRoot.Add(Node);
  self.Notify(fRoot);
end;

constructor TModelIndexTree.Create;
begin
end;

destructor TModelIndexTree.Destroy;
begin
  freeAndNil(fRoot);
  inherited;
end;

end.

